# generate_human_optimal_states.py

import json
import os

# 定义人类最优参数
manual_params = [
    {'learning_rate': 0.01, 'max_depth': 3, 'n_estimators': 50},
    {'learning_rate': 0.01, 'max_depth': 4, 'n_estimators': 75},
    {'learning_rate': 0.02, 'max_depth': 5, 'n_estimators': 100},
    {'learning_rate': 0.02, 'max_depth': 6, 'n_estimators': 125},
    {'learning_rate': 0.05, 'max_depth': 3, 'n_estimators': 100},
    {'learning_rate': 0.05, 'max_depth': 5, 'n_estimators': 150},
    {'learning_rate': 0.1, 'max_depth': 4, 'n_estimators': 75},
]

def load_evaluation_cache(evaluation_cache_file='evaluation_cache.json'):
    """
    加载evaluation_cache.json文件，返回字典。
    """
    if os.path.exists(evaluation_cache_file):
        with open(evaluation_cache_file, 'r') as f:
            cache = json.load(f)
        print(f"Loaded evaluation cache from {evaluation_cache_file}.")
        return cache
    else:
        print(f"No evaluation cache found at {evaluation_cache_file}.")
        return {}

def get_accuracy(cache, params):
    """
    根据超参数组合从缓存中获取准确率。
    """
    params_str = json.dumps(params, sort_keys=True)
    return cache.get(params_str, None)

def generate_human_optimal_states(manual_params, cache, output_file='human_optimal_states.json'):
    """
    生成human_optimal_states.json文件。
    """
    human_optimal_states = []
    missing_params = []

    for params in manual_params:
        accuracy = get_accuracy(cache, params)
        if accuracy is not None:
            state = params.copy()
            state['accuracy'] = accuracy
            human_optimal_states.append(state)
        else:
            missing_params.append(params)

    if missing_params:
        print("警告：以下人类参数在evaluation_cache.json中未找到对应的准确率。")
        for params in missing_params:
            print(params)

    # 保存到JSON文件
    with open(output_file, 'w') as f:
        json.dump(human_optimal_states, f, indent=4)
    print(f"已生成 {output_file}，包含 {len(human_optimal_states)} 条人类最优状态。")

if __name__ == "__main__":
    evaluation_cache = load_evaluation_cache('evaluation_cache.json')
    generate_human_optimal_states(manual_params, evaluation_cache, 'human_optimal_states.json')
